package cn.tedu;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class Demo09 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入用户名");
        String username = sc.nextLine();
        System.out.println("输入密码");
        String password = sc.nextLine();

        try (Connection conn = DBUtils.getConn()){
            String sql = "select password from user where username=?";
//          预编译避免SQL注入
            PreparedStatement ps = conn.prepareStatement(sql);
//            替换?
            ps.setString(1,username);
//            执行SQL语句
            ResultSet rs = ps.executeQuery();
//            判断是否查询到数据
            if (rs.next()){
                String pw = rs.getString(1);
                if (pw.equals(password)) {
                    System.out.println("登录成功");
                }else {
                    System.out.println("密码错误");
                }
            }else {
                System.out.println("用户名错误");
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
